package com.song.orderservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.commonutils.JwtUtils;
import com.song.commonutils.ResultObj;
import com.song.orderservice.entity.TOrder;
import com.song.orderservice.service.TOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-04-06
 */
@RestController
@RequestMapping("/orderservice/order")
@Api(description = "订单管理")
public class TOrderController {

    @Autowired
    private TOrderService tOrderService;

    /**
     * 创建订单
     * @param courseId
     * @param request
     * @return
     */
    @ApiOperation("创建订单")
    @PostMapping("/{courseId}")
    public ResultObj saveOrder(@PathVariable String courseId, HttpServletRequest request){
        //用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        String orderNo=tOrderService.createOrders(courseId,id);
        return ResultObj.ok().data("data",orderNo);
    }

    /**
     * 根据订单id查询订单信息
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单id查询订单信息")
    @GetMapping("/getOrderInfo/{orderId}")
    public  Object getOrderInfo(@PathVariable String orderId){
        QueryWrapper<TOrder> qw=new QueryWrapper<>();
        qw.eq("order_no",orderId);
        TOrder order = tOrderService.getOne(qw);
        return ResultObj.ok().data("data",order);
    }

    @ApiOperation("根据课程id查询订单表中课程状态")
    @GetMapping("/isByCourse/{courseId}")
    public boolean isByCourse(@PathVariable String courseId,HttpServletRequest request){
        QueryWrapper<TOrder> qw=new QueryWrapper();
        qw.eq("course_id",courseId);
        qw.eq("member_id",JwtUtils.getMemberIdByJwtToken(request));
        qw.eq("status",1);
        int count = tOrderService.count(qw);
        if(count>0){
            return true;
        }
        return false;
    }

}

